package gw.com.android.ui;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.model.ConfigType;
import www.com.library.model.DataItemResult;
import www.com.library.view.RecyclerClickListener;

/**
 * Created by reeta.zou on 2016/7/13.
 *
 */
public class SimpleItemAdapter extends RecyclerView.Adapter{
    private DataItemResult mArray;
    private LayoutInflater mInflater = null;
    private RecyclerClickListener mClickListener = null;

    public SimpleItemAdapter(Activity mActivity, DataItemResult mArray, RecyclerClickListener mCallback) {
        mInflater = mActivity.getLayoutInflater();
        this.mArray = mArray;
        mClickListener = mCallback;
    }

    /**
     * 返回界面显示的选项文案
     * @param position
     * @return
     */
    public String getItem(int position) {
        if (null != mArray) {
            if(position >= 0 && position < mArray.getDataCount()){
                return mArray.getItem(position).getString(ConfigType.CONFIG_TYPE_TITLE_TAG);
            }
        }
        return "";
    }

    /**
     * 返回选项的唯一标识key
     * @param position
     * @return
     */
    public String getKey(int position){
        if (null != mArray) {
            if(position >= 0 && position < mArray.getDataCount()){
                return mArray.getItem(position).getString(ConfigType.CONFIG_TYPE_KEY_TAG);
            }
        }
        return null;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.list_item_system_arraw, parent,false);
        return new MenuHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MenuHolder menuHolder = (MenuHolder) holder;
        String mItem = getItem(position);
        if(null != mItem){
            menuHolder.itemView.setTag(position);
            menuHolder.mTextview.setText(mItem);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (null != mArray) {
            return mArray.getDataCount();
        }
        return 0;
    }

    /**
     * item视图控件对象
     *
     * @author Administrator
     *
     */
    class MenuHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_title)
        TextView mTextview;

        public MenuHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.item_layout)
        void onViewClick(){
            if(null != mClickListener){
                int position = (int) itemView.getTag();
                mClickListener.onClick(position, mArray.getItem(position));
            }
        }
    }
}
